package cn.org.donald.framework.controller;


import cn.org.donald.framework.constant.InfoConstant;
import cn.org.donald.framework.constant.TableConstant;
import cn.org.donald.framework.pojo.dto.AddRoleMenuDto;
import cn.org.donald.framework.pojo.vo.HttpResult;
import cn.org.donald.framework.service.SysRoleMenuService;
import cn.org.donald.framework.service.SysRoleService;
import cn.org.donald.pojo.entity.system.RoleMenu;
import cn.org.donald.framework.base.BaseController;
import cn.org.donald.framework.util.ListUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Donald
 * @since 2020-11-10
 */
@Controller
@RequestMapping("/sysRoleMenu")
public class SysRoleMenuController extends BaseController {

    @Reference(version = "1.0.0",group = "donald")
    private SysRoleMenuService sysRoleMenuService;

    @Reference(version = "1.0.0",group = "donald")
    private SysRoleService sysRoleService;

    @PostMapping("/add")
    @ApiOperation(value = "根据角色id添加菜单集合")
    @RequiresPermissions("sysRoleMenu:add:post")
    public HttpResult add(@RequestBody AddRoleMenuDto addRoleMenuDto){
        if (TableConstant.VALUE_ADMIN_NAME.equals(addRoleMenuDto.getRoleName())){
            return toResultFail(InfoConstant.ADMIN_ROLE_EXCEPT_INFO);
        }
        if (ListUtil.isEmptyOrHasClassFieldAllEmpty(addRoleMenuDto.getMenuList())){
            return toResultFail("没有可添加的菜单");
        }
        return sysRoleMenuService.insertBatch(addRoleMenuDto)?toResultSuccess(InfoConstant.ADD_SUCCESS):toResultFail(InfoConstant.ADD_FAIL);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除角色已分配的菜单")
    @RequiresPermissions("sysRoleMenu:delete:post")
    public HttpResult delete(@RequestBody RoleMenu roleMenu){
        if (sysRoleService.isAdmin(roleMenu.getRoleId())){
            return toResultFail(InfoConstant.ADMIN_ROLE_EXCEPT_INFO);
        }
        if (roleMenu.getMenuId() == null || roleMenu.getRoleId() == null){
            return toResultFail("没有可移除的菜单");
        }
        boolean flag = sysRoleMenuService.delete(roleMenu);
        return flag? toResultSuccess(InfoConstant.DELETE_SUCCESS):toResultFail(InfoConstant.DELETE_FAIL);
    }
}

